/*
 *
 *  Copyright (C) 2010 GSyC/LibreSoft, Universidad Rey Juan Carlos
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Juan Francisco Gato Luis <jfcogato@libresoft.es>
 *
 */

package com.LibreGeoSocial.App;

import java.io.File;
import java.io.FileOutputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

import com.LibreGeoSocial.Api.LibreGeoSocial;

public class ViewPhoto extends Activity{

	private static final int MENU_SHARE = Menu.FIRST + 1;
	
	private ImageView imageUser;
	private TextView lbTitle;
	private String url, name, data;
	private Bitmap photo;
	
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.view_photo);
		
        Intent i = getIntent();
		
        lbTitle = (TextView) this.findViewById(R.id.lbTitle);
        imageUser = (ImageView) this.findViewById(R.id.lbImage);
        
		url = i.getStringExtra("url");
		name = i.getStringExtra("name");
		data = i.getStringExtra("data");

        photo = LibreGeoSocial.getInstance().getBitmap(url+"medium/");
        
        
        
        imageUser.setImageBitmap(photo);
        
        lbTitle.setText (name);


	}
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	
        super.onCreateOptionsMenu(menu);
        menu.add(0, MENU_SHARE, 0, "Share")
                .setAlphabeticShortcut('A');
                 		        
        return true;
    }
	
    @Override
    public boolean onOptionsItemSelected (MenuItem item) {
    	
    	switch (item.getItemId()) {
    	
    		case MENU_SHARE:

    			String tmp_path = getResources().getString(R.string.tmp_path);

                //create the dir if not exists
                File dirhandler = new File (tmp_path);
                if(! dirhandler.exists())
                  dirhandler.mkdirs();

                try {
                    FileOutputStream out = new FileOutputStream( tmp_path + name.replace(" ", "_") + ".jpeg");
                            photo.compress(Bitmap.CompressFormat.JPEG, 100, out);
                            out.flush();
                            out.close();
                    } catch (Exception e) {
                            e.printStackTrace();
                    }

                Intent i = new Intent(Intent.ACTION_SEND);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.putExtra(Intent.EXTRA_TEXT, data);
                i.setType("image/jpeg");
                i.putExtra(Intent.EXTRA_SUBJECT, "[LGS-Photo] " + name);
                i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File( tmp_path + name.replace(" ", "_") + ".jpeg")));
                startActivity(i);

       	}

    	return super.onOptionsItemSelected(item);
    }
}
